package com.platform.controller.sso;

import com.alibaba.fastjson.JSON;
import com.platform.base.BaseController;
import com.platform.common.ServerResponse;
import com.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 单点登录请求控制层
 */
@RestController
@RequestMapping("/sso/")
@CrossOrigin
@PropertySource(value = "classpath:platform.properties")
public class SsoController extends BaseController {


    @Value("${USER_SESSION_KEY}")
    private String USER_SESSION_KEY;


    /**
     * 根据token及用户查询缓存信息
     * @param token
     * @param userName
     * @return
     */
    @PostMapping(value = "login")
    public ServerResponse<Map> loginByTokenAndUserName(@RequestParam(value = "token") String token,
                                                             @RequestParam(value = "userName") String userName){
        String result = iJedisClientService.get(USER_SESSION_KEY+":"+token);
        if(StringUtil.isEmpty(result)){
            return ServerResponse.createByErrorMessage("token值已失效");
        }
        Map resultMap = JSON.parseObject(result,Map.class);
        String tokenUserName = StringUtil.valueOfString(resultMap.get("username"));
        if(!tokenUserName.equals(userName)){
            return ServerResponse.createByErrorMessage("token与用户名不匹配");
        }
        iPsacService.selectByUserName(userName);
        return ServerResponse.createBySuccess("匹配成功",resultMap);
    }

}
